package com.steel.yfmall.cloud.assistant.impl;

import com.steel.yfmall.cloud.assistant.api.stock.EndCodeBeanServiceAPI;
import com.steel.yfmall.cloud.assistant.bean.param.APIParamEndCodeBean;
import com.steel.yfmall.cloud.assistant.bean.result.APIResultEndCodeBean;
import com.steel.yfmall.cloud.assistant.po.view.EndCodeBean;
import com.steel.yfmall.cloud.assistant.po.view.EndCodeBeanExample;
import com.steel.yfmall.cloud.assistant.view.EndCodeBeanService;
import com.steel.yfmall.cloud.assistant.dto.para.view.ParaEndCodeBean;
import com.steel.yfmall.cloud.assistant.dto.result.view.ReusltEndCodeBean;
import com.steel.yfmall.cloud.assistant.view.mapper.EndCodeBeanMapper;
import com.steel.yfmall.cloud.components.utils.base.APIListBean;
import com.steel.yfmall.cloud.components.utils.base.ResponseCode;
import com.steel.yfmall.cloud.components.utils.utils.MobileBaseBeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 查询终点代码业务逻辑 对外接口实现
 *
 * @author jia chang bao
 * @date 2020/12/3 15:17
 */
@Service
@Slf4j
public class EndCodeBeanServiceAPIImpl implements EndCodeBeanServiceAPI {

    @Resource
    EndCodeBeanMapper endCodeBeanMapper;


    /**
     * 根据条件查询终点代码信息（不分页）
     *
     * @param apiParamEndCodeBean 查询终点代码业务条件
     * @return 终点代码信息结果（不分页）
     * @author jia chang bao
     */
    @Override
    public APIListBean<APIResultEndCodeBean> getEndCodeOfList(APIParamEndCodeBean apiParamEndCodeBean) {
        APIListBean<APIResultEndCodeBean> apiListBean = new APIListBean<>();
        if (ObjectUtils.isEmpty(apiParamEndCodeBean)) {
            apiListBean.setData(null);
            apiListBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("参数不能为空"));
            return apiListBean;
        }

        if (StringUtils.isBlank(apiParamEndCodeBean.getCity()) && StringUtils.isBlank(apiParamEndCodeBean.getConty()) && StringUtils.isBlank(apiParamEndCodeBean.getProvince())) {
            apiListBean.setData(null);
            apiListBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("至少传递省、市、区县参数中的一个"));
            return apiListBean;
        }
        ParaEndCodeBean paraEndCodeBean = new ParaEndCodeBean();
        MobileBaseBeanUtils.copyAllFields(apiParamEndCodeBean, paraEndCodeBean);

        List<ReusltEndCodeBean> endCodeOfList = endCodeBeanService.getEndCodeOfList(paraEndCodeBean);
        if (!CollectionUtils.isEmpty(endCodeOfList)) {
            List<APIResultEndCodeBean> list = new ArrayList<>();
            for (ReusltEndCodeBean reusltEndCodeBean : endCodeOfList) {
                APIResultEndCodeBean apiResultEndCodeBean = new APIResultEndCodeBean();
                MobileBaseBeanUtils.copyAllFields(reusltEndCodeBean, apiResultEndCodeBean);
                list.add(apiResultEndCodeBean);
            }
            apiListBean.setData(list);
            apiListBean.setSoaStateBean(ResponseCode.buildEnumSuccessSOAStateBean("查询终点代码成功"));
            return apiListBean;
        }
        apiListBean.setData(null);
        apiListBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("未查询到终点代码"));
        return apiListBean;
    }

    /**
     * 根据条件批量查询终点代码信息
     *
     * @param apiParamEndCodeBean 查询终点代码业务条件
     * @return 终点代码信息结果（不分页）
     */
    @Override
    public APIListBean<APIResultEndCodeBean> getEndCodeBySiteNoOfList(APIParamEndCodeBean apiParamEndCodeBean) {
        APIListBean<APIResultEndCodeBean> apiListBean = new APIListBean<>();
        if (ObjectUtils.isEmpty(apiParamEndCodeBean)) {
            apiListBean.setData(null);
            apiListBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("参数不能为空"));
            return apiListBean;
        }

        EndCodeBeanExample endCodeBeanExample = new EndCodeBeanExample();
        EndCodeBeanExample.Criteria criteria = endCodeBeanExample.createCriteria();
        criteria.andSiteNoIn(apiParamEndCodeBean.getSiteNoList());
        List<EndCodeBean> endCodeOfList = endCodeBeanMapper.selectByExample(endCodeBeanExample);

        if (!CollectionUtils.isEmpty(endCodeOfList)) {
            List<APIResultEndCodeBean> list = new ArrayList<>();
            for (EndCodeBean reusltEndCodeBean : endCodeOfList) {
                APIResultEndCodeBean apiResultEndCodeBean = new APIResultEndCodeBean();
                MobileBaseBeanUtils.copyAllFields(reusltEndCodeBean, apiResultEndCodeBean);
                list.add(apiResultEndCodeBean);
            }
            apiListBean.setData(list);
            apiListBean.setSoaStateBean(ResponseCode.buildEnumSuccessSOAStateBean("查询终点代码成功"));
            return apiListBean;
        }
        apiListBean.setData(null);
        apiListBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("未查询到数据"));
        return apiListBean;
    }

    /**
     * 终到地点
     */
    @Resource
    private EndCodeBeanService endCodeBeanService;
}
